package cn.ekuma.epos.analysis.front.customerAction.model;

import java.util.ArrayList;
import java.util.List;

import org.jdesktop.swingx.treetable.AbstractTreeTableModel;
import org.jdesktop.swingx.treetable.TreeTableNode;

import com.openbravo.pos.base.AppLocal;

public class ProductCategoryTreeTableModel extends AbstractTreeTableModel {

	public static String[] COLUMNNAME_ProductCategoryAnalysisInfo={
		 AppLocal.getIntString("label.prodcategory"),
		 AppLocal.getIntString("label.units"),
		 AppLocal.getIntString("label.prodpricebuy"),
	     AppLocal.getIntString("label.prodpricesell"),
	     AppLocal.getIntString("label.profits")
	 };
	
    public ProductCategoryTreeTableModel(TreeTableNode root) {
        super(root);
    }

    @Override
    public String getColumnName(int column) {
        return COLUMNNAME_ProductCategoryAnalysisInfo[column];
      
    }

    @Override
    public int getColumnCount() {
        return COLUMNNAME_ProductCategoryAnalysisInfo.length;
    }

    @Override
    public Object getValueAt(Object node, int column) {
        if (column < 0 || column >= getColumnCount()) {
            throw new IllegalArgumentException("column must be a valid index");
        }
        TreeTableNode ttn = (TreeTableNode) node;
        if (column >= ttn.getColumnCount()) {
            return null;
        }
        return ttn.getValueAt(column);
    }

    @Override
    public Object getChild(Object parent, int index) {
        return ((TreeTableNode) parent).getChildAt(index);
    }

    @Override
    public int getChildCount(Object parent) {
        return ((TreeTableNode) parent).getChildCount();
    }

    @Override
    public int getIndexOfChild(Object parent, Object child) {
        return ((TreeTableNode) parent).getIndex((TreeTableNode) child);
    }

    public void reflush(TreeTableNode rootNode) {
        super.root=rootNode; 
        modelSupport.fireNewRoot();
    }

    public TreeTableNode[] getPathToRoot(TreeTableNode aNode) {
        List<TreeTableNode> path = new ArrayList<TreeTableNode>();
        TreeTableNode node = aNode;

        while (node != root) {
            path.add(0, node);
            node = (TreeTableNode) node.getParent();
        }
        if (node == root) {
            path.add(0, node);
        }
        return path.toArray(new TreeTableNode[0]);
    }
}
